package com.shop2cn.iapi.agentproducts.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.QueryProductUsedBean;
import com.shop2cn.iapi.agentproducts.service.QueryProductUsedCall;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightTemplateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.AgentUsers;
/**
*查询运费模板是否被商品使用
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryProductUsed{
	private static QueryProductUsedBean queryproductusedBean;
	private static QueryProductUsedCall queryproductusedCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询运费模板是否被商品使用");
	}

	@Before
	public void caseUp() {
		queryproductusedBean=new QueryProductUsedBean();
		queryproductusedCall=new QueryProductUsedCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryproductusedCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductUsed_01() {
		Logger.start(true,"查询运费模板是否被商品使用,正在使用");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(createmchBean.getMchId(),createmchBean.getNickName(),false);
			addAgentProductBean.getCatalogList().forEach(x->x.setWeight(5.00));
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 2, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);
			addAgentProductBean.setFreightTemplateId(Integer.parseInt(templateId));

			AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(2000);
			queryproductusedBean.setTemplateId(Integer.parseInt(templateId));
			queryproductusedBean.setUserId(createmchBean.getMchId());
			queryproductusedCall.setData(queryproductusedBean);
			queryproductusedCall.callService();			
			Logger.verifyEquals("200",queryproductusedCall.getString("code"),"验证code");

			Logger.verifyEquals(true,queryproductusedCall.getJsonObject().get("data").getAsJsonObject().get("used").getAsBoolean(),"验证used");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductUsed_02() {
		Logger.start(true,"查询运费模板是否被商品使用,没有在使用");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 2, 2);
			String templateId = GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);

			queryproductusedBean.setTemplateId(Integer.parseInt(templateId));
			queryproductusedBean.setUserId(createmchBean.getMchId());
			queryproductusedCall.setData(queryproductusedBean);
			queryproductusedCall.callService();
			Logger.verifyEquals("200",queryproductusedCall.getString("code"),"验证code");
			Logger.verifyEquals(false,queryproductusedCall.getJsonObject().get("data").getAsJsonObject().get("used").getAsBoolean(),"验证used");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductUsed_03() {
		Logger.start(true,"templateId为空");
		try {
			queryproductusedBean.setUserId(AgentUsers.rootUserId);
			queryproductusedCall.setData(queryproductusedBean);
			queryproductusedCall.callService();
			Logger.verifyEquals("200",queryproductusedCall.getString("code"),"验证code");
			Logger.verifyEquals(false,queryproductusedCall.getJsonObject().get("data").getAsJsonObject().get("used").getAsBoolean(),"验证used");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}